package com.bjfu.eduservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bjfu.common_utils.JwtUtils;
import com.bjfu.common_utils.R;
import com.bjfu.common_utils.vo.UcenterMemberVo;
import com.bjfu.eduservice.client.UcenterClient;
import com.bjfu.eduservice.entity.EduComment;
import com.bjfu.eduservice.service.EduCommentService;
import com.bjfu.servicebase.exceptionhandler.GuliException;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;

/**
 * <p>
 * 评论 前端控制器
 * </p>
 *
 * @author Guli
 * @since 2020-07-17
 */
@Api(tags = "课程评论")
@RestController
@RequestMapping("/eduservice/comment")
//@CrossOrigin
public class EduCommentController {

    @Autowired
    private EduCommentService commentService;

    @Autowired
    private UcenterClient ucenterClient;

    @ApiOperation("评论分页列表")
    @GetMapping("getCommentPageList/{page}/{limit}")
    public R getCommentPageList(
            @ApiParam("当前页码")
            @PathVariable long page,
            @ApiParam("每页记录数")
            @PathVariable long limit,
            @ApiParam("课程id")
            String courseId
    ){
        // 构造查询条件
        QueryWrapper<EduComment> wrapper = new QueryWrapper<>();
        wrapper.eq("course_id",courseId);
        Page<EduComment> commentPage = new Page<>(page,limit);
        wrapper.orderByDesc("gmt_create");
        // 构造分页器
        commentService.page(commentPage,wrapper);
        HashMap<String, Object> paramsMap = new HashMap<>();
        paramsMap.put("pages",commentPage.getPages());
        paramsMap.put("total",commentPage.getTotal());
        paramsMap.put("size",commentPage.getSize());
        paramsMap.put("records",commentPage.getRecords());
        paramsMap.put("current",commentPage.getCurrent());
        paramsMap.put("previous",commentPage.hasPrevious());
        paramsMap.put("next",commentPage.hasNext());
        return R.ok().data(paramsMap);
    }

    @ApiOperation("添加评论")
    @PostMapping("addComment")
    public R addComment(
            @ApiParam("课程评论")
            @RequestBody EduComment comment,
            @ApiParam("http请求头")
            HttpServletRequest request) {
        String token = (String) request.getHeader("token");
        if (token == null) {
            throw new GuliException(28004,"请先登录");
        }
        String memberId = JwtUtils.getMemberIdByJwtToken(request);
        UcenterMemberVo userInfo = ucenterClient.getMemberInfo(memberId);
        //System.out.println(userInfo);
        comment.setAvatar(userInfo.getAvatar());
        if(comment.getMemberId() != userInfo.getId()){
            throw new GuliException(28004,"账户信息不匹配,存在重大安全隐患");
        }
        comment.setMemberId(userInfo.getId());
        comment.setNickname(userInfo.getNickname());
        commentService.save(comment);
        return R.ok();
    }
}

